<template>
  <div class="login-page">
    <div class="fullscreen-bg" aria-hidden="true"></div>
    <div class="login-card">
      <h1>登录</h1>
      <p class="subtitle">选择你的身份进入平台</p>

      <a-form ref="formRef" :model="form" :rules="rules" class="form">
        <a-form-item name="username">
          <a-input v-model:value="form.username" placeholder="用户名" />
        </a-form-item>

        <a-form-item name="password">
          <a-input-password v-model:value="form.password" placeholder="密码" />
        </a-form-item>

        <a-form-item name="role">
          <a-radio-group v-model:value="form.role" class="role-group">
            <a-radio-button value="student">学生任务中心</a-radio-button>
            <a-radio-button value="teacher">老师端中心</a-radio-button>
          </a-radio-group>
        </a-form-item>

        <a-button type="primary" class="submit" @click="onSubmit" :loading="loading" block>登录</a-button>
      </a-form>

      <div class="quick-login">
        <a-button @click="simulateLogin('student')" :loading="loading">模拟学生登录</a-button>
        <a-button @click="simulateLogin('teacher')" :loading="loading">模拟老师登录</a-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { setAuth } from '../utils/auth'

const router = useRouter()
const route = useRoute()
const loading = ref(false)
const formRef = ref()
const form = reactive({ username: '', password: '', role: 'student' })

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  role: [{ required: true, message: '请选择登录中心', trigger: 'change' }]
}

const onSubmit = () => {
  formRef.value?.validate(async (valid) => {
    if (!valid) return
    loading.value = true
    try {
      // 这里可替换为真实登录接口
      await new Promise(r => setTimeout(r, 500))
      setAuth({ username: form.username, role: form.role })
      const redirect = route.query.redirect
      if (typeof redirect === 'string' && redirect) {
        router.replace(redirect)
      } else {
        router.replace(form.role === 'teacher' ? '/teacher' : '/student-tasks')
      }
    } finally {
      loading.value = false
    }
  })
}

const simulateLogin = async (role) => {
  loading.value = true
  try {
    await new Promise(r => setTimeout(r, 300))
    setAuth({ username: role === 'teacher' ? '王老师' : '张三同学', role })
    const redirect = route.query.redirect
    if (typeof redirect === 'string' && redirect) {
      router.replace(redirect)
    } else {
      router.replace(role === 'teacher' ? '/teacher' : '/student-tasks')
    }
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.fullscreen-bg { position: fixed; inset: 0; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); z-index: -1; }

.login-card {
  width: 100%;
  max-width: 420px;
  background: rgba(255, 255, 255, 0.96);
  border-radius: 16px;
  padding: 28px 24px 22px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.15);
}
.login-card h1 { margin: 0; font-size: 1.6rem; color: #2c3e50; text-align: center; }
.login-card .subtitle { text-align: center; color: #5a6c7d; margin: 8px 0 18px; }

.form :deep(.el-input__wrapper) { padding: 4px 12px; }
.role-group { width: 100%; display: flex; justify-content: center; }
.submit { width: 100%; margin-top: 4px; box-shadow: 0 8px 18px rgba(102, 126, 234, 0.35); }

.quick-login { margin-top: 12px; display: flex; gap: 10px; justify-content: space-between; }
</style>

